#include <iostream>
#include <chrono>
#include <thread>

class ControlPanel {
private:
    int password[4];    // 存储密码
    int input[4];       // 存储用户输入的密码
    int inputIndex;     // 存储用户当前输入的密码的位置
    bool locked;        // 是否被锁定
    std::chrono::system_clock::time_point lockStartTime; // 锁定开始的时间
public:
    ControlPanel() {
        // 初始化密码
        for (int i = 0; i < 4; i++) {
            password[i] = i + 1;
        }

        inputIndex = 0;
        locked = false;
    }

    void enterPassword(int key) {
        // 如果已经被锁定，则判断是否可以解锁
        if (locked) {
            std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
            std::chrono::duration<double> elapsedSeconds = now - lockStartTime;
            if (elapsedSeconds.count() >= 120) {
                locked = false;
                std::cout << "Control panel unlocked." << std::endl;
            } else {
                std::cout << "Control panel is locked. Please try again later." << std::endl;
                return;
            }
        }

        // 在输入密码期间，控制面板处于等待用户输入状态
        if (inputIndex < 4) {
            input[inputIndex] = key; // 存储输入的字符
            inputIndex++;

            // 如果输入完整的密码，则进行验证
            if (inputIndex == 4) {
                bool passwordCorrect = true; // 假设输入密码正确
                for (int i = 0; i < 4; i++) {
                    if (input[i] != password[i]) {
                        passwordCorrect = false; // 输入密码错误
                        break;
                    }
                }

                if (passwordCorrect) {
                    std::cout << "Password correct. Control panel unlocked." << std::endl;
                    inputIndex = 0; // 重置输入索引
                    // 进入系统功能选择状态
                    // ...
                } else {
                    std::cout << "Password incorrect. Please try again." << std::endl;
                    inputIndex = 0; // 重置输入索引
                    static int failedAttempts = 0; // 记录失败的尝试次数
                    failedAttempts++;
                    if (failedAttempts == 3) {
                        locked = true;
                        lockStartTime = std::chrono::system_clock::now();
                        std::cout << "Control panel is locked. Please try again later." << std::endl;
                    }
                }
            }
        }
    }
};

int main() {
    ControlPanel controlPanel;
    // 模拟用户输入密码的过程
    controlPanel.enterPassword(1);
    controlPanel.enterPassword(2);
    controlPanel.enterPassword(3);
    controlPanel.enterPassword(4);
    controlPanel.enterPassword(5);
    controlPanel.enterPassword(6);
    controlPanel.enterPassword(7);
    controlPanel.enterPassword(8);
    controlPanel.enterPassword(3);
    controlPanel.enterPassword(5);
    controlPanel.enterPassword(2);
    return 0;
}